package com.example.juejincheck.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.tea.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author yangkun
 * @description
 * @date 2022年12月29日 10:08
 */
@Slf4j
public class TianxingUtils {

    /**
     * 获取每日早安寄语
     *
     */
    public static String getGoodMorning() {
        String path = "zaoan/index";
        //调用每日简报的接口
        String body = cn.hutool.http.HttpUtil
                .createGet(Constants.TIAN_XING_API + path + "?key=" + Constants.TIAN_KEY)
                .execute()
                .body();
        JSONObject jsonObjectResult = JSON.parseObject(body);
        Integer code = jsonObjectResult.getInteger("code");
        if (code != 200) {
            log.error("请求每日新闻的接口出错，出错信息：" + body);
            return "一切都是最好的安排~";
        }
        log.info("请求每日新闻的接口成功：" + body);
        JSONObject resultObject = jsonObjectResult.getJSONObject("result");
        return resultObject.getString("content");
    }
    /**
     * 获取每日土味情话
     *
     */
    public static String getLoveWords(){
        String path = "saylove/index";
        //调用每日简报的接口
        String body = cn.hutool.http.HttpUtil
                .createGet(Constants.TIAN_XING_API + path + "?key=" + Constants.TIAN_KEY)
                .execute()
                .body();
        JSONObject jsonObjectResult = JSON.parseObject(body);
        Integer code = jsonObjectResult.getInteger("code");
        if(code!=200){
            log.error("请求每日新闻的接口出错，出错信息："+body);
            return "一切都是最好的安排~";
        }
        JSONObject resultObject = jsonObjectResult.getJSONObject("result");
        log.info("请求每日新闻的接口成功："+resultObject);
        return resultObject.getString("content");
    }
    /**
     * 获取每日土味情话
     *
     */
    public static String getQingshiWords(){
        String path = "qingshi/index";
        //调用每日简报的接口
        String body = cn.hutool.http.HttpUtil
                .createGet(Constants.TIAN_XING_API + path + "?key=" + Constants.TIAN_KEY)
                .execute()
                .body();
        JSONObject jsonObjectResult = JSON.parseObject(body);
        Integer code = jsonObjectResult.getInteger("code");
        if(code!=200){
            log.error("请求每日新闻的接口出错，出错信息："+body);
            return "一切都是最好的安排~";
        }
        JSONObject resultObject = jsonObjectResult.getJSONObject("result");
        log.info("请求每日新闻的接口成功："+resultObject);
        return resultObject.getString("content");
    }
    /**
     * 获取每日土味情话
     *
     */
    public static String getCaipu(String caipu){
        String path = "caipu/index";
        //调用每日简报的接口
        String body = cn.hutool.http.HttpUtil
                .createGet(Constants.TIAN_XING_API + path + "?key=" + Constants.TIAN_KEY+"&word=" + caipu+"&num=10")
                .execute()
                .body();
        JSONObject jsonObjectResult = JSON.parseObject(body);
        Integer code = jsonObjectResult.getInteger("code");
        if(code!=200){
            log.error("请求每日新闻的接口出错，出错信息："+body);
            return "";
        }
        JSONObject resultObject = jsonObjectResult.getJSONObject("result");
        JSONArray list = resultObject.getJSONArray("list");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = list.getJSONObject(i);
            sb.append((i+1)+"、")
                    .append(jsonObject.getString("cp_name"))
                    .append("：\n\n")
                    .append("【做法】")
                    .append(jsonObject.getString("zuofa"))
                    .append("\n\n")
                    .append("【调料】")
                    .append(StringUtils.isEmpty(jsonObject.getString("tiaoliao")) ? "无": jsonObject.getString("tiaoliao"))
                    .append("\n\n")
                    .append("【原料】")
                    .append(jsonObject.getString("yuanliao"))
                    .append("\n\n")
            ;
        }
        log.info("请求每日新闻的接口成功："+resultObject);
        return sb.toString();
    }

    /**
     * 获取天气预报
     *
     */
    public static JSONObject getWeather(){
        String path = "tianqi/index";
        //调用每日简报的接口
        String body = cn.hutool.http.HttpUtil
                .createGet(Constants.TIAN_XING_API + path + "?key=" + Constants.TIAN_KEY+"&type=1&city=410500")
                .execute()
                .body();
        JSONObject jsonObjectResult = JSON.parseObject(body);
        Integer code = jsonObjectResult.getInteger("code");
        if(code!=200){
            log.error("请求每日新闻的接口出错，出错信息："+body);
            return null;
        }

        return jsonObjectResult.getJSONObject("result");
        /*
        * {
          "code": 200,
          "msg": "success",
          "result": {
            "date": "2022-12-19",
            "week": "星期一",
            "province": "上海",
            "area": "上海",
            "areaid": "101020100",
            "weather": "晴",
            "weatherimg": "qing.png",
            "weathercode": "qing",
            "real": "3℃",
            "lowest": "2℃",
            "highest": "9℃",
            "wind": "北风",
            "windspeed": "0",
            "windsc": "0级",
            "sunrise": "06:47",
            "sunset": "16:54",
            "moonrise": "",
            "moondown": "",
            "pcpn": "0",
            "uv_index": "0",
            "aqi": "54",
            "quality": "良",
            "vis": "18",
            "humidity": "48",
            "alarmlist": [

            ],
            "tips": "晴天紫外线等级较高，外出注意补水防晒。天气凉，适宜着一到两件羊毛衫、大衣、毛套装、皮夹克等春秋着装；年老体弱者宜着风衣加羊毛衫等厚型春秋着装。空气质量较好，可以参与各类户外活动。疫情防控不松懈，出门请佩戴口罩。"
              }
            }
        * */
    }

    public static void main(String[] args) {
        System.out.println(getGoodMorning());
        System.out.println(getLoveWords());
        System.out.println(getQingshiWords());
    }
}
